﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DACU.Properties;
using DACU.Tools;

namespace DACU.VkontakteApi
{
	class VKException:Exception
	{
		private int _errorCode;
		public int ErrorCode
		{
			get { return _errorCode; }
		}

		private string _description;
		public string Description
		{
			get { return _description; }
		}

		public VKException()
		{
		}

		public VKException(int errorCode)
		{
			_errorCode = errorCode;
			#region switch (errorCode)
			switch (errorCode)
			{
				case 1:
					_description = Resources.VKException_1;
					break;
				case 2:
					_description = Resources.VKException_2;
					break;
				case 3:
					_description = Resources.VKException_3;
					break;
				case 4:
					_description = Resources.VKException_4;
					break;
				case 5:
					_description = Resources.VKException_5;
					break;
				case 6:
					_description = Resources.VKException_6;
					break;
				case 7:
					_description = Resources.VKException_7;
					break;
				case 8:
					_description = Resources.VKException_8;
					break;
				case 100:
					_description = Resources.VKException_100;
					break;
				case 113:
					_description = Resources.VKException_113;
					break;
				case 114:
					_description = Resources.VKException_114;
					break;
				case 117:
					_description = Resources.VKException_117;
					break;
				case 118:
					_description = Resources.VKException_118;
					break;
				case 119:
					_description = Resources.VKException_119;
					break;
				case 120:
					_description = Resources.VKException_120;
					break;
				case 121:
					_description = Resources.VKException_121;
					break;
				case 123:
					_description = Resources.VKException_123;
					break;
				case 148:
					_description = Resources.VKException_148;
					break;
				case 170:
					_description = Resources.VKException_170;
					break;
				case 200:
				case 201:
					_description = Resources.VKException_200;
					break;
				case 202:
					_description = Resources.VKException_202;
					break;
				case 301:
					_description = Resources.VKException_301;
					break;
				case 302:
					_description = Resources.VKException_302;
					break;
				case 500:
					_description = Resources.VKException_VKException_500;
					break;
				default:
					_description = Resources.VKException_default + ": " + ErrorCode;
					break;
			}
			#endregion
		}

		public static VKException ParseResponse(string response)
		{
			XElement xDoc;
			//try
			//{
				xDoc = XElement.Parse(response, LoadOptions.None);
			//}
			//catch
			//{
			//    throw;
			//}
			return ParseXml(xDoc);
		}

		public static VKException ParseXml(XElement response)
		{
			if (response == null || response.IsEmpty) return new VKException(1);
			var errorCode = response.Element("error_code");
			return errorCode != null && !errorCode.IsEmpty
			       	? new VKException(Convert.ToInt32(errorCode.Value))
			       	: new VKException(1);
		}

		public override string ToString()
		{
			return "ErrorCode: {0}\nDescription: {1}\n".FormatStr(ErrorCode,Description) + base.ToString();
		}
	}
}
